<template>
  <div class="user-management">
    <el-container>
      <el-header>
        <h2>用户管理</h2><!--
        <el-input placeholder="按用户名搜索" class="search-input"></el-input>
        <el-button type="primary">搜索</el-button>-->
      </el-header>
      <el-main>
        <el-button type="primary" @click="openDialog('add')">新增用户</el-button>
        <el-table :data="users" style="width: 100%">
          <el-table-column prop="userId" label="ID" width="80"></el-table-column>
          <el-table-column prop="username" label="用户名" width="180"></el-table-column>
          <el-table-column prop="email" label="邮箱"></el-table-column>
          <el-table-column label="操作" width="180">
            <template slot-scope="scope">
              <el-button size="mini" @click="openDialog('edit', scope.row)">编辑</el-button>
              <el-button size="mini" type="danger" @click="deleteUser(scope.row.userId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-dialog :title="dialogTitle" :visible.sync="dialogVisible">
        <el-form :model="form">
          <el-form-item label="用户名">
            <el-input v-model="form.username"></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input type="password" v-model="form.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱">
            <el-input v-model="form.email"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="submitForm">确定</el-button>
        </div>
      </el-dialog>
    </el-container>
  </div>
</template>
<script>
import axios from 'axios';

export default {
  data() {
    return {
      users: [],
      form: {
        userId: null,
        username: '',
        password: '',
        email: ''
      },
      dialogVisible: false,
      dialogTitle: '',
      searchKeyword: '',
      searchResults: [],
      displayedUsers: []
    };
  },
  created() {
    this.fetchUsers();
   /* this.displayedUsers = [...this.users];*/
  },
  methods: {
    fetchUsers() {
      axios.get('/user/list').then(response => {
        this.users = response.data;
    /*    this.displayedUsers = [...this.users];*/
      });
    },

    openDialog(type, user = null) {
      this.dialogTitle = type === 'add' ? '新增用户' : '编辑用户';
      this.dialogVisible = true;
      if (user) {
        this.form = { ...user };
      } else {
        this.form = { userId: null, username: '', password: '', email: '' };
      }
    },
    submitForm() {
      let url = '/user/add';
      let method = 'post';
      if (this.form.userId) {
        url = '/user/update';
        method = 'put';
      }
      axios[method](url, this.form).then(() => {
        this.dialogVisible = false;
        this.fetchUsers();
      }).catch(error => {
        console.error('Error during form submission:', error)
      });
    },
    deleteUser(userId) {
      axios.delete(`/user/delete/${userId}`).then(() => {
        this.fetchUsers();
      });
    }
  }
};
</script>

<style scoped>

.user-management {
  padding: 20px;
}
.el-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  text-align: center;
}
.search-input {
  width: 200px;
}
</style>
